package com.buptlogistics.creditinvestigation.fragment;

import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.buptlogistics.creditinvestigation.R;
import com.buptlogistics.creditinvestigation.activity.MainActivity;
import com.buptlogistics.creditinvestigation.app.BaseFragment;
import com.buptlogistics.creditinvestigation.utils.ToastUtil;

/**
 * 包名：com.buptlogistics.creditinvestigation.fragment
 * 类描述：应收账款
 * 创建人：fuxuemingzhu
 * 邮箱：fuxuemingzhu@163.com
 * 创建时间：2015/12/30 11:14
 * <p/>
 * 修改人：fuxuemingzhu
 * 修改时间：2015/12/30 11:14
 * 修改备注：
 *
 * @version 1.0
 */
public class DeserveCheckFragment extends BaseFragment {

    private EditText et_derserve_company, et_derserve_money;
    private Button btn_deserve_next;

    private View view;


    public DeserveCheckFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_deserve_check, container, false);
        initViews();
        initEvents();
        return view;
    }

    protected void initViews() {
        et_derserve_company = (EditText) findViewById(R.id.et_deserve_check_money);
        et_derserve_money = (EditText) findViewById(R.id.et_deserve_detail_money);
        btn_deserve_next = (Button) findViewById(R.id.btn_deserve_next);
    }

    protected void initEvents() {
        btn_deserve_next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder builder = new AlertDialog.Builder(DeserveCheckFragment.this
                        .getActivity());
                builder.setMessage("您确定向" + "xx" + "企业发送应收账款融资申请吗？");
                builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
                    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Intent intent = new Intent(DeserveCheckFragment.this.getActivity(), MainActivity.class);
                        // 启动intent对应的Activity
                        startActivity(intent);
                        DeserveCheckFragment.this.getActivity().finishAffinity();
                        initStateBar();
                    }
                });
                builder.setNegativeButton("否", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        ToastUtil.showToast(DeserveCheckFragment.this.getActivity(), "否");
                    }
                });
                builder.show();

            }
        });
    }

    private void initStateBar() {
        NotificationManager mNotificationManager = (NotificationManager)
                DeserveCheckFragment.this.getActivity().getSystemService
                        (Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(DeserveCheckFragment.this.getActivity());
        mBuilder.setContentTitle("您的应收账款融资申请已通过")//设置通知栏标题
                .setContentText("您的应收账款融资申请已通过，请查看")
                .setContentIntent(getDefaultIntent(Notification.FLAG_AUTO_CANCEL)) //设置通知栏点击意图
//	.setNumber(number) //设置通知集合的数量
                .setTicker("测试通知来啦") //通知首次出现在通知栏，带上升动画效果的
                .setWhen(System.currentTimeMillis())//通知产生的时间，会在通知信息里显示，一般是系统获取到的时间
                .setPriority(Notification.PRIORITY_DEFAULT) //设置该通知优先级
//	.setAutoCancel(true)//设置这个标志当用户单击面板就可以让通知将自动取消
                .setOngoing(false)//ture，设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)
                .setDefaults(Notification.DEFAULT_VIBRATE)//向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置，使用defaults属性，可以组合
                        //Notification.DEFAULT_ALL  Notification.DEFAULT_SOUND 添加声音 // requires VIBRATE permission
                .setSmallIcon(R.drawable.ic_launcher);//设置通知小ICON
        mNotificationManager.notify(Notification.FLAG_AUTO_CANCEL, mBuilder.build());
    }

    //提醒标志符，向通知添加声音、闪灯和振动效果等设置达到通知提醒效果，可以组合多个属性
    public PendingIntent getDefaultIntent(int flags) {
        Bundle data = new Bundle();
        // 创建一个Intent
        Intent intent = new Intent(DeserveCheckFragment.this.getActivity(),
                MainActivity.class);
        intent.putExtras(data);
        // 启动intent对应的Activity
        return PendingIntent.getActivity(this.getActivity(), 1, intent, flags);
    }

    protected View findViewById(int id) {
        return view.findViewById(id);
    }

}
